package com.mine.jzh3.oss.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * OSS并发任务线程池配置类
 * 专用于OSS批量复制、上传等IO密集型任务
 */
@Slf4j
@Configuration
public class OssThreadPoolConfig {

    /**
     * OSS目录复制专用线程池
     */
    @Bean(name = "ossCopyExecutor")
    public ThreadPoolTaskExecutor ossCopyExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        int cores = Runtime.getRuntime().availableProcessors();
        executor.setCorePoolSize(cores * 2);        // 核心线程数（I/O密集建议2倍CPU）
        executor.setMaxPoolSize(cores * 4);         // 最大线程数
        executor.setQueueCapacity(200);             // 任务等待队列容量
        executor.setKeepAliveSeconds(60);           // 非核心线程空闲存活时间
        executor.setThreadNamePrefix("oss-copy-");  // 线程名统一前缀

        // 拒绝策略：任务过载时由调用线程执行，防止任务丢弃
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        executor.initialize();
        log.info("OSS线程池初始化成功，核心线程数: {}, 最大线程数: {}", executor.getCorePoolSize(), executor.getMaxPoolSize());
        return executor;
    }
}
